package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址管理接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.addAddressBook(addressBook);
        return Result.success("新增地址成功！");
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result setAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.setAddressBook(addressBook);
        return Result.success("修改地址成功！");
    }


    @GetMapping("/default")
    @ApiOperation("获取用户默认地址")
    public Result<AddressBook> getDefaultAddressBook(){
        AddressBook addressBook=addressBookService.getDefaultAddressBook();
        return Result.success(addressBook);
    }


    @GetMapping("/list")
    @ApiOperation("获取用户所有地址")
    public Result<List<AddressBook>> listAddressBook(){
        List<AddressBook> addressBooks=addressBookService.listAddressBook();
        return Result.success(addressBooks);
    }


    @PutMapping("/default")
    @ApiOperation("修改用户默认地址")
    public Result<String> setDefaultAddressBook(@RequestBody AddressBook addressBook){
        log.info("当前修改默认地址的用户id：{}",addressBook.getId());
        addressBookService.setDefaultAddressBook(addressBook.getId());
        return Result.success("修改用户默认地址成功！");
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址信息")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }


    @DeleteMapping
    @ApiOperation("删除地址")
    public Result<String> delAddressBook(Long id){
        addressBookService.del(id);
        return Result.success("删除地址成功！");
    }
}
